package trivia;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import javax.swing.JTextPane;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.JLabel;

import questionary.Question;
import questionary.Questionary;

import myExceptions.MyFileException;

public class EditFrame {

	private JFrame jEditQFrame = null; 
	private JFrame jChooseQFrame = null;
	private JPanel jEditQContentPane = null;
	@SuppressWarnings("unused")
	private JScrollPane jEditScrollPane = null;
	private ChooseQtypeFrame typeQ = null;  //  @jve:decl-index=0:
	private JButton jSaveButton = null;
	private JButton jCancelButton = null;
	private JButton jNewButton = null;
	private JList jEditList = null;
	
	@SuppressWarnings("unused")
	private String questionText;
	private JTextPane jQuestionEditTextArea = null;  //  @jve:decl-index=0:visual-constraint="-63,103"
	private JTextPane [] jOptions = null;
	private JTextPane [] jAnswers = null;
	private JTextPane jScore = null;
	Question singleQ;
	Questionary q;
	
	private int y = 195, offset = 35;
	@SuppressWarnings("unused")
	private JLabel jOptionsLabel = null;  //  @jve:decl-index=0:visual-constraint="52,195"
	@SuppressWarnings("unused")
	private JLabel jAnswersLabel = null;
	@SuppressWarnings("unused")
	private JLabel jScoreLabel = null;
	
	public EditFrame (Questionary q) {
		this.q = q;
		jEditQFrame = getJEditQFrame();
		jEditQFrame.setVisible(false);
		jChooseQFrame = getJChooseQFrame();
		jChooseQFrame.setVisible(false);
	}
	
	

	public JFrame getJChooseQFrame() {
		if (jChooseQFrame == null) {
			jChooseQFrame = new JFrame();
			jChooseQFrame.setSize(new Dimension(528, 124));
			jChooseQFrame.setTitle("Select a question ...");
			jChooseQFrame.getContentPane().add(new JScrollPane(getJEditList()));
			
		}
		return jChooseQFrame;
	}



	/**
	 * This method initializes jNewButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJNewButton() {
		if (jNewButton == null) {
			jNewButton = new JButton();
			jNewButton.setText("New");
			jNewButton.setBounds(new Rectangle(435, 90, 120, 30));
			
			jNewButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					typeQ = new ChooseQtypeFrame();
					typeQ.getJChooseQtypeFrame().setVisible(true);
					typeQ.getJOkButton().addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							jEditQContentPane = null;
							jEditQFrame.setContentPane(getJEditQContentPane());
													
							typeQ.setOptionsCount(Integer.parseInt(typeQ.getOptionsComboBoxSelected()));
							typeQ.setAnswerCount(Integer.parseInt(typeQ.getAnswerComboBoxSelected()));
							try {
								typeQ.setQuestion();
							} catch (MyFileException e1) {
								e1.printStackTrace();
							}
							
							singleQ = typeQ.getQuestion();
							
							//TODO debug
							System.out.println("chequeando question obtenido");
							//singleQ.showQuestion();
							
							y = 195;
							jQuestionEditTextArea = null;
							jEditQContentPane.add(jQuestionEditTextArea = getJQuestionEditTextPane());
		                	jOptions = getJArrayTextArea(singleQ.getOptions(), singleQ.getOptionsCount(), "Options");
		                	jAnswers = getJArrayTextArea(singleQ.getAnswers(), singleQ.getCorrectOptionsCount(), "Answer");
		                	jScore = getJArrayTextArea(singleQ.getScore(), "Score");
		                	setJNewEditQContentPane(jOptions, singleQ.getOptionsCount());
		                	setJNewEditQContentPane(jAnswers, singleQ.getCorrectOptionsCount());
		                	setJNewEditQContentPane(jScore);
		                	jEditQFrame.setSize(600, 400+(singleQ.getCorrectOptionsCount()+singleQ.getOptionsCount())*35);
		                	jEditQFrame.setVisible(true);
		                	jChooseQFrame.setVisible(false);
						}
					});

				}
			});
		}
		return jNewButton;
	}

	/**
	 * This method initializes jEditQFrame	
	 * 	
	 * @return javax.swing.JFrame	
	 */
	public JFrame getJEditQFrame() {
		if (jEditQFrame == null) {
			jEditQFrame = new JFrame();
			jEditQFrame.setSize(new Dimension(600, 500));
			jEditQFrame.setTitle("Edit");
			jEditQFrame.setContentPane(getJEditQContentPane());
			
		}
		return jEditQFrame;
	}

	/**
	 * This method initializes jEditQContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 * 
	 *  JScrollPane scrollPane = new JScrollPane(dataList);
 // Or in two steps:
 JScrollPane scrollPane = new JScrollPane();
 scrollPane.getViewport().setView(dataList);

	 */
	private JPanel getJEditQContentPane() {
		if (jEditQContentPane == null) {
			jEditQContentPane = new JPanel();
			jEditQContentPane.setLayout(null);
			jEditQContentPane.add(getJNewButton(), null);
			jEditQContentPane.add(getJSaveButton(), null);
			jEditQContentPane.add(getJCancelButton(), null);
		}
		return jEditQContentPane;
	}
	
	private void setJNewEditQContentPane(JTextPane [] array, int n) {
		for (int i=0; i < n; i++) {
			jEditQContentPane.add(array[i]);
		}
		jEditQFrame.setContentPane(jEditQContentPane);
	}
	
	private void setJNewEditQContentPane(JTextPane singleTextPane) {
		jEditQContentPane.add(singleTextPane);
		jEditQFrame.setContentPane(jEditQContentPane);
	}

	/**
	 * This method initializes jSaveButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJSaveButton() {
		if (jSaveButton == null) {
			jSaveButton = new JButton();
			jSaveButton.setText("Save");
			jSaveButton.setBounds(new Rectangle(435, 135, 120, 30));
			jSaveButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						saveQuestion();
					} catch (MyFileException exc) {
						// TODO Bloque catch generado automticamente
						new ErrorFrame(exc.getMessage());
					}
					jEditQFrame.dispose();
				}
			});
		}
		return jSaveButton;
	}
	
	private void saveQuestion () throws MyFileException {
		
		singleQ.setQuestion(jQuestionEditTextArea.getText());
		singleQ.setOptions(toArray(jOptions));
		singleQ.setAnswers(toArray(jAnswers));
		singleQ.setDiffic(q.getDifficulty());
		
		try {
			singleQ.setScore(Integer.parseInt(jScore.getText()));
		} catch (NumberFormatException e){
			throw new MyFileException("Score format incorrect!");
		}
		
		q.addQuestion(singleQ);
	}

	/**
	 * This method initializes jCancelButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJCancelButton() {
		if (jCancelButton == null) {
			jCancelButton = new JButton();
			jCancelButton.setText("Cancel");
			jCancelButton.setBounds(new Rectangle(435, 180, 120, 30));
			jCancelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jEditQFrame.setVisible(false);
				}
			});
		}
		return jCancelButton;
	}

	/**
	 * This method initializes jEditList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJEditList() {
		if (jEditList == null) {
			jEditList = new JList();
			jEditList.setBounds(new Rectangle(17, 18, 404, 50));
			jEditList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			jEditList.setListData(q.getAllQsummary());
			jEditList.addMouseListener(new MouseAdapter() {
	            public void mouseClicked(MouseEvent e) {
	                if (e.getClickCount() == 2) {
	                	singleQ =  q.getEditQ(jEditList.getSelectedIndex());
	                	jEditQContentPane.add(jQuestionEditTextArea = getJQuestionEditTextPane());
	                	jOptions = getJArrayTextArea(singleQ.getOptions(), singleQ.getOptionsCount(), "Options");
	                	jAnswers = getJArrayTextArea(singleQ.getAnswers(), singleQ.getCorrectOptionsCount(), "Answer");
	                	jScore = getJArrayTextArea(singleQ.getScore(), "Score");
	                	setJNewEditQContentPane(jOptions, singleQ.getOptionsCount());
	                	setJNewEditQContentPane(jAnswers, singleQ.getCorrectOptionsCount());
	                	setJNewEditQContentPane(jScore);
	                	jEditQFrame.setVisible(true);
	                	jChooseQFrame.setVisible(false);
	                }
	            }
	        });

		}
		return jEditList;
	}

	/**
	 * This method initializes jQuestionEditTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextPane getJQuestionEditTextPane() {
		if (jQuestionEditTextArea == null) {
			jQuestionEditTextArea = new JTextPane();
			jQuestionEditTextArea.setBounds(new Rectangle(17, 18, 404, 162));
			jQuestionEditTextArea.setText(singleQ.getQuestion());
		}
		return jQuestionEditTextArea;
	}
	
	private JTextPane [] getJArrayTextArea (String [] src, int size, String title) {
		JTextPane [] array = new JTextPane [size];
		jEditQContentPane.add(getJLabel(17, y, 100, 25, title));
		y +=offset;
		for (int i = 0; i < size; i++, y+=offset) {
			array[i] = new JTextPane();
			array[i].setBounds(new Rectangle(18, y, 404, 25));
			array[i].setText(src[i]);
		}

		return array;
	}
	
	private JTextPane  getJArrayTextArea (Integer src, String title) {
		jEditQContentPane.add(getJLabel(17, y, 100, 25, title));
		y +=offset;
		JTextPane  ret = new JTextPane ();
		ret.setBounds(new Rectangle(18, y, 404, 25));
		ret.setText(src.toString());
		return ret;
	}

	
	private JLabel getJLabel(int x, int y, int w, int h, String text) {
		JLabel label = new JLabel(text);
		label.setBounds(new Rectangle(x, y, w, h));
		label.setText(text);
		return label;
	}
	
	private String [] toArray (JTextPane [] array) {
		LinkedList<String> ret = new LinkedList <String> ();
		
		//TODO DEBUG
		String [] aux = null;
		
		for (int i = 0; i < array.length; i++) {
			ret.add(array[i].getText());
		}
		
		aux = new String [array.length];
		ret.toArray(aux);
		
		for (int i = 0; i < aux.length; i++) {
			System.out.println(aux[i]);
		}
		
		
		return aux;
		
	}

}
